<?php
class Controller_User extends Controller_Template{
	public $template="template/cloudlecturing";
	public function before()
	{
		parent::before();
		if($this->auto_render)
		{
			$this->template->title='';
			$this->template->content='';
			$this->template->styles=array();
			$this->template->scripts=array();
		}

	}

	public function after()
	{
		parent::after();
	}

	public function action_index()
	{
		Request::current()->redirect('user/login');
	}
	public function action_login()
	{
		if(Auth::instance()->logged_in())
		{
			Request::current()->redirect('user/profile');
		}
		$this->template->title=__('login');
		$this->template->content=View::factory('login');
		if($_POST)
		{
			if(Auth::instance()->login($_POST['username'],$_POST['password'],false))
			{

				Request::current()->redirect('user/profile'); // login ok
				
			}
			else
			{
				//$this->template->content->errors = $_POST->errors('login');
/*				$user=ORM::factory('user',array('username'=>$_POST['username']));
				echo $user->has('roles', ORM::factory('role', array('name' => 'login'))) . "<br>";
				echo $user->password . "<br>";
				echo hash_hmac('sha256',$_POST['password'],'secret');
				echo 'error';
*/
//				Request::current()->redirect(); // login false 
			}
		}
	}
	public function action_register()
	{

		if(Auth::instance()->logged_in())
		{
			Request::current()->redirect('user/profile');
		}
		$this->template->title=__('register');
		$this->template->content=View::factory('register');

		if($_POST)
		{
			
		
			$user=ORM::factory('user');
			$user->create_user($_POST,array('username','password','email'));
			$login_role=new Model_Role(array('name'=>'login'));
			$user->add('roles',$login_role);

		}
	}
	public function action_profile()
	{
		$this->template->content='profile';
	}
}
?>
